"""更新授权状态

Revision ID: 933d922a7edc
Revises: a3a951d38327
Create Date: 2025-01-21 17:16:38.328315

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '933d922a7edc'
down_revision: Union[str, None] = 'a3a951d38327'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### 首先创建枚举类型 ###
    op.execute("CREATE TYPE authstatusenum AS ENUM ('PENDING', 'APPROVED', 'REJECTED')")
    op.execute("CREATE TYPE authtypeenum AS ENUM ('REAL_NAME', 'EDUCATION')")
    
    # ### 然后修改列类型 ###
    op.execute("ALTER TABLE user_auth ALTER COLUMN auth_status TYPE authstatusenum USING auth_status::authstatusenum")
    op.execute("ALTER TABLE user_auth ALTER COLUMN auth_type TYPE authtypeenum USING auth_type::authtypeenum")
    
    # ### 其他列的修改保持不变 ###
    op.alter_column('user_auth', 'img_list',
               existing_type=sa.VARCHAR(length=255),
               nullable=True,
               existing_comment='材料')
    op.alter_column('user_auth', 'remarks',
               existing_type=sa.VARCHAR(length=255),
               nullable=True,
               existing_comment='备注')


def downgrade() -> None:
    # ### 先将列转换回VARCHAR ###
    op.alter_column('user_auth', 'remarks',
               existing_type=sa.VARCHAR(length=255),
               nullable=False,
               existing_comment='备注')
    op.alter_column('user_auth', 'img_list',
               existing_type=sa.VARCHAR(length=255),
               nullable=False,
               existing_comment='材料')
    
    op.execute("ALTER TABLE user_auth ALTER COLUMN auth_type TYPE VARCHAR(20) USING auth_type::VARCHAR")
    op.execute("ALTER TABLE user_auth ALTER COLUMN auth_status TYPE VARCHAR(20) USING auth_status::VARCHAR")
    
    # ### 删除枚举类型 ###
    op.execute("DROP TYPE IF EXISTS authtypeenum")
    op.execute("DROP TYPE IF EXISTS authstatusenum")
